package com.ysd.lis.controller.sys;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ysd.common.Result;
import com.ysd.lis.annotation.LogDetail;
import com.ysd.lis.aspect.OperationType;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.SysBasDict;
import com.ysd.lis.entity.SysBasDictDetail;
import com.ysd.lis.mapper.sys.SysBasDictMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.util.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 员工表
 * 前端控制器
 * </p>
 *
 * @author LiuQi
 * @since 2023-10-19
 */
@Api(tags = "基础字典明细")
@RestController
@RequestMapping("/api/sys/basDictDetail")
public class SysBasDictDetailController extends BaseController {

    @Autowired
    SysBasDictDetailService sysBasDictDetailService;
    @Autowired
    SysBasDictMapper sysBasDictMapper;

    @ApiOperation("查询")
    @GetMapping("/findDictDetailList")
    public Result findDictDetailList(CommonPageDto pageParam) {
        return sysBasDictDetailService.findDictDetailList(pageParam);
    }

    @ApiOperation("查询")
    @PostMapping("/findDictDetails")
    public String findDictDetails(@RequestBody String code) {
        return sysBasDictDetailService.findDictDetails(code);
    }

    @ApiOperation("查询")
    @GetMapping("/findDictDetailListByDictCodes")
    public Result findDictDetailListByDictCodes(CommonPageDto pageParam) {
        return sysBasDictDetailService.findDictDetailListByDictCodes(pageParam);
    }

    @ApiOperation("批量新增")
    @PostMapping("/addBatchDetail")
    @LogDetail(requestParam = "addBatchDetail", operationType = OperationType.INSERT, module = "字典设计", content = "通过字典设计，新建【name】字典值", businessName = "name")
    public Result addBatchDetail(@RequestBody SysBasDict dict) {
        return sysBasDictDetailService.addBatchDetail(dict);
    }

    @ApiOperation("批量修改")
    @PostMapping("/editBatchDetail")
    @LogDetail(requestParam = "editBatchDetail", operationType = OperationType.UPDATE, module = "字典设计", content = "通过字典设计，编辑【name】字典值", businessName = "name")
    public Result editBatchDetail(@RequestBody SysBasDict dict) {
        return sysBasDictDetailService.editBatchDetail(dict);
    }

    @ApiOperation("批量添加或修改")
    @PostMapping("/saveOrUpdateDetail")
    @LogDetail(requestParam = "saveOrUpdateDetail", operationType = OperationType.UPDATE, module = "字典设计", content = "通过字典设计，编辑【name】字典值", businessName = "name")
    public Result saveOrUpdateDetail(@RequestBody SysBasDict dict) {
        return sysBasDictDetailService.saveOrUpdateDetail(dict);
    }

    @ApiOperation("根据详情id删除详情数据")
    @PostMapping("/deleteBatchDetail")
    @LogDetail(requestParam = "deleteBatchDetail", operationType = OperationType.DELETE, module = "字典设计", content = "通过字典设计，删除【name】字典值", businessName = "name")
    public Result deleteBatchDetail(@RequestBody SysBasDictDetail detail) {
        return sysBasDictDetailService.deleteBatchDetail(detail);
    }

    @ApiOperation("上传字典")
    @PostMapping("/uploadDetailByExcel")
    @LogDetail(operationType = OperationType.UPDATE, module = "上传字典")
    public Result uploadDetailByExcel(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) throws Exception {
        return sysBasDictDetailService.uploadDetailByExcel(file, request);
    }

    @ApiOperation("导出字典")
    @GetMapping("/export")
    @LogDetail(operationType = OperationType.UPDATE, module = "导出字典")
    public void export(String pid, HttpServletResponse response) {
        SysBasDict sysBasDict = sysBasDictMapper.selectById(pid);

        LambdaQueryWrapper<SysBasDict> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysBasDict::getDelFlag, 0);
        lambdaQueryWrapper.eq(SysBasDict::getPid, pid);

        lambdaQueryWrapper.orderByAsc(SysBasDict::getSx);
        List<SysBasDict> dictList = sysBasDictMapper.selectList(lambdaQueryWrapper);
        List<Object> codeList = dictList.stream().map(SysBasDict::getCode).collect(Collectors.toList());

        // 查询数据
        CommonPageDto commonPageDto = new CommonPageDto();
        commonPageDto.setPid(pid);
        Result dictDetailList = sysBasDictDetailService.findDictDetailList(commonPageDto);
        List<Map<String, Object>> dataList = (List<Map<String, Object>>) dictDetailList.getData();

        // 将数据汇总
        List<List<Object>> sheetDataList = new ArrayList<>();
        sheetDataList.add(codeList);

        for (Map<String, Object> map : dataList) {
            List<Object> data = new ArrayList<>();
            for (Object headCode : codeList) {
                String value = (String) map.get(headCode);
                data.add(value);
            }
            sheetDataList.add(data);
        }
        // 导出数据
        ExcelUtils.export(response, sysBasDict.getName(), sheetDataList);
    }
}
